//
//  BaseViewController.m
//  JJHW_coreData
//
//  Created by lizhenqiang on 16/4/2.
//  Copyright © 2016年 HFKJ_LZQ. All rights reserved.
//

#import "BaseViewController.h"
#import "BaseNavigationController.h"
#import "XNTabBarController.h"
#import "LoginViewController.h"

typedef NS_ENUM(NSInteger, AnalyseMethodType)
{
    AnalyseMethodTypeJustRefresh = 0,
    AnalyseMethodTypeLazyCreate,
    AnalyseMethodTypeForceCreate
};



@interface BaseViewController ()<UIGestureRecognizerDelegate>
{
    
}

@end

@implementation BaseViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = kColorTableBG;
    self.automaticallyAdjustsScrollViewInsets = NO;//这个方法在scollview加在self.view中很重要
//    self.labelTitleView =[[UILabel alloc] initWithFrame:CGRectMake(80, 0, DeviceWidth-170, 30)];//44
//    self.labelTitleView.text =self.titleForNav;
//    self.labelTitleView.backgroundColor =[UIColor clearColor];
//    self.labelTitleView.font =[UIFont systemFontOfSize:17.0];
//    self.labelTitleView.textAlignment =NSTextAlignmentCenter;
    [self setNavTitleView];
    
    if ([self.navigationController.viewControllers count]>1)
    {
        UIBarButtonItem *leftItem =[[UIBarButtonItem alloc] init];
        UIButton *btnleftView = [[UIButton alloc] initWithFrame:CGRectMake(-20, 0, 14, 44)];//10, 0, 67, 57;
//        UIImageView *upImageView = [[UIImageView alloc] initWithFrame:CGRectMake(-5, 11, 13, 23)];
//        upImageView.image = [UIImage imageNamed:@"icon_back"];
        
        UIButton *imageBtn = [[UIButton alloc] initWithFrame:CGRectMake(-5, 11, 13, 23)];
        [imageBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        [btnleftView addSubview:imageBtn];
        imageBtn.userInteractionEnabled = YES;
        [imageBtn addTarget:self action:@selector(PopNav:) forControlEvents:UIControlEventTouchUpInside];
//        [btnleftView setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
//        [btnleftView addSubview:upImageView];
//        [btnleftView setContentMode:UIViewContentModeScaleAspectFit];
        [leftItem  setCustomView:btnleftView];
        [btnleftView addTarget:self action:@selector(PopNav:) forControlEvents:UIControlEventTouchUpInside];
        UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(PopNav:)];
        single.numberOfTapsRequired = 1;
//        upImageView.userInteractionEnabled = YES;
//        [upImageView addGestureRecognizer:single];
        self.navigationItem.leftBarButtonItem =leftItem;
        
        self.navigationController.interactivePopGestureRecognizer.delegate =(id<UIGestureRecognizerDelegate>)self;
    }
    else
    {
        //    self.viewDeckController.delegate =self;
    }
    if (self.isSupportSingleTapHideKeyboard)
    {
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
        tap.cancelsTouchesInView = NO;
        [self.view addGestureRecognizer:tap];
    }
    self.labelNoData =[[UILabel alloc] init];
    self.labelNoData.textColor =kDefaultFontGrayColor;
    self.labelNoData.textAlignment =NSTextAlignmentCenter;
    self.labelNoData.font =themeFont14;
    self.labelNoData.hidden =YES;
}


- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
    if (self.navigationController&&[self.navigationController.viewControllers count]==1) {//处理navigationcontroller rootViewController 的侧滑事件
        self.navigationController.interactivePopGestureRecognizer.enabled =NO;
    }
    else {
        self.navigationController.interactivePopGestureRecognizer.enabled =YES;
    }
}

- (void)PopNav:(id)sender
{
    if ([self.navigationController.viewControllers count]==1)
    {
        
    }
    else
    {
        //        [[HZMAPImanager shareMAPImanager] removeDelegate:self];
        [self.navigationController popViewControllerAnimated:YES];
    }
}
- (void)NavPop:(UIPanGestureRecognizer *)sender
{
    if ([self.navigationController.viewControllers count]==1)
    {
        
    }
    else
    {
        CGPoint translatedPoint = [sender translationInView:self.view];
       
        if (translatedPoint.x>0.0f)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    //
//    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:YES];
    
    // [self.navigationController.navigationBar lt_reset];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setNavTitleView
{
    self.labelTitleView =[[UILabel alloc] initWithFrame:CGRectMake(80, 0, DeviceWidth-170, 30)];//44
    self.labelTitleView.text =self.titleForNav;
    self.labelTitleView.backgroundColor =[UIColor clearColor];
    self.labelTitleView.font =[UIFont systemFontOfSize:17.0];
    self.labelTitleView.textAlignment =NSTextAlignmentCenter;
    self.labelTitleView.textColor = NavBarTitleColor;
    self.navigationItem.titleView =self.labelTitleView;
}

- (void)setRightNavItemWith:(NavBarItem *)obj /*andNavItem:(UINavigationItem *)NavItem*/
{
    UINavigationItem *NavItem = self.navigationItem;
    switch (obj.type) {
        case NavBarItemTypeOnlyStr:
        {
            UIBarButtonItem *rightItem =[[UIBarButtonItem alloc] initWithTitle:obj.title_normal style:UIBarButtonItemStyleBordered target:self action:@selector(rightItemClick:)];
            [rightItem setTintColor:NavBarTitleColor];
            NavItem.rightBarButtonItems = @[rightItem];
            
            
        }
            break;
        case NavBarItemTypeCanChangeStr:
        {
            UIButton *rightBtn = [[UIButton alloc] initWithFrame:obj.btnFrame];//
            [rightBtn setTitle:obj.title_normal forState:UIControlStateNormal];
            [rightBtn setTitle:obj.title_selected forState:UIControlStateSelected];
            [rightBtn setTitleColor:NavBarItemColor_normal forState:UIControlStateNormal];
            [rightBtn setTitleColor:NavBarItemColor_selected forState:UIControlStateSelected];
            [rightBtn addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
            //    [_rightView addSubview:btnShare];
            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
            NavItem.rightBarButtonItem = rightItem;
        }
            break;
        case NavBarItemTypeImage:
        {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
            
//            UIButton *rightBtn = [[UIButton alloc] initWithFrame:obj.btnFrame];//
            UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake((view.frame.size.width-obj.btnFrame.size.width)+7, (view.frame.size.height-obj.btnFrame.size.height)/2.0, obj.btnFrame.size.width, obj.btnFrame.size.height)];//
            [view addSubview:rightBtn];
            [rightBtn setImage:obj.normalImage forState:UIControlStateNormal];
            [rightBtn setImage:obj.selectImage forState:UIControlStateSelected];
            [rightBtn setImage:obj.normalImage forState:UIControlStateHighlighted];
            [rightBtn addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
            //    [_rightView addSubview:btnShare];
//            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
            UITapGestureRecognizer *single = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(rightItemClick:)];
            single.numberOfTapsRequired = 1;
            view.userInteractionEnabled = YES;
//            view.backgroundColor = [UIColor redColor];
            [view addGestureRecognizer:single];
            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
            NavItem.rightBarButtonItem = rightItem;
            
        }
            break;
        case NavBarItemTypeStrAndImage:
        {//因为选中城市过来需要改变 label的内容（显示不同的城市）
//            CGFloat width = [NSString calculateTextWidth:22 Content:obj.title_normal font:themeFont17];//这个是title的长度
//            UIView *view = [[UIView alloc] initWithFrame:obj.btnFrame];
//            
//            
//            UIButton *rightBtn = [[UIButton alloc] initWithFrame:obj.btnFrame];//
//            [rightBtn setImage:obj.normalImage forState:UIControlStateNormal];
//            [rightBtn setImage:obj.selectImage forState:UIControlStateSelected];
//            [rightBtn setImage:obj.normalImage forState:UIControlStateHighlighted];
//            [rightBtn addTarget:self action:@selector(rightItemClick:) forControlEvents:UIControlEventTouchUpInside];
//            //    [_rightView addSubview:btnShare];
//            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:view];
//            NavItem.rightBarButtonItem = rightItem;
            
        }
            break;
            
        default:
            break;
    }
}

- (void)setLeftNavItemWith:(NavBarItem *)obj /*andNavItem:(UINavigationItem *)NavItem*/
{
    UINavigationItem *NavItem = self.navigationItem;
    switch (obj.type) {
        case NavBarItemTypeOnlyStr:
        {
            UIBarButtonItem *rightItem =[[UIBarButtonItem alloc] initWithTitle:obj.title_normal style:UIBarButtonItemStyleBordered target:self action:@selector(leftItemClick:)];
            [rightItem setTintColor:NavBarTitleColor];
            NavItem.leftBarButtonItems = @[rightItem];
        }
            break;
        case NavBarItemTypeCanChangeStr:
        {
            UIButton *rightBtn = [[UIButton alloc] initWithFrame:obj.btnFrame];//
            [rightBtn setTitle:obj.title_normal forState:UIControlStateNormal];
            [rightBtn setTitle:obj.title_selected forState:UIControlStateSelected];
            [rightBtn setTitleColor:NavBarItemColor_normal forState:UIControlStateNormal];
            [rightBtn setTitleColor:NavBarItemColor_selected forState:UIControlStateSelected];
            [rightBtn addTarget:self action:@selector(leftItemClick:) forControlEvents:UIControlEventTouchUpInside];
            //    [_rightView addSubview:btnShare];
            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
            NavItem.leftBarButtonItem = rightItem;
        }
            break;
        case NavBarItemTypeImage:
        {
            UIButton *rightBtn = [[UIButton alloc] initWithFrame:obj.btnFrame];//
            [rightBtn setImage:obj.normalImage forState:UIControlStateNormal];
            [rightBtn setImage:obj.normalImage forState:UIControlStateHighlighted];
            [rightBtn setImage:obj.selectImage forState:UIControlStateSelected];
            [rightBtn addTarget:self action:@selector(leftItemClick:) forControlEvents:UIControlEventTouchUpInside];
            //    [_rightView addSubview:btnShare];
            UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
            NavItem.leftBarButtonItem = rightItem;
            
        }
            break;
            
        default:
            break;
    }
}

- (void)rightItemClick:(id)sender
{
    
}
- (void)leftItemClick:(id)sender
{
    
}
- (void)hideKeyboard
{
    [self.view endEditing:YES];
    
}

- (void)addLabelNoDataToView:(UIView *)view AndText:(NSString *)str  andFrame:(CGRect)rect
{
    self.labelNoData.frame =rect;
    self.labelNoData.text =str;
    [view addSubview:self.labelNoData];
}
- (void)isHideLabelNoDataWith:(BOOL)hide
{
    self.labelNoData.hidden =hide;
}
- (void)dealloc
{
    CDLog(@"%@dealloc",self.debugDescription);
}
- (void)setTableFooterView:(UITableView *)tb {
    if (!tb) {
        return;
    }
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor whiteColor];
    [tb setTableFooterView:view];
}

- (void)setTableViewFooterView:(UITableView *)tb andColor:(UIColor *)color
{
    if (!tb) {
        return;
    }
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = color;
    [tb setTableFooterView:view];
}

-(BOOL)isLogin
{
    Person *person =[Person sharePerson];
    // person.isLogined =[[NSUserDefaults standardUserDefaults] boolForKey:knotNeedLogin];
    if (person.isLogined)
    {
        return YES;
    }else
    {
        LoginViewController *login = [[LoginViewController alloc] initWithNibName:@"LoginViewController" bundle:nil];
        BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:login];
        [self presentViewController:nav animated:YES completion:nil];
        return NO;
    }
}






#pragma mark Mycoding 方法

- (void)tabBarItemClicked
{
    CDLog(@"\ntabBarItemClicked : %@", NSStringFromClass([self class]));
}

+ (UIViewController *)presentingVC //这个方法是可以见鉴的。。
{
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *result = window.rootViewController;
    while (result.presentedViewController)//找到这个viewController的最上层
    {
        result = result.presentedViewController;
    }
    
    if ([result isKindOfClass:[XNTabBarController class]])
    {
        result = [(XNTabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]])
    {
        result = [(UINavigationController *)result topViewController];
    }
    return result;
}

+ (void)presentVC:(UIViewController *)viewController
{
    if (!viewController)
    {
        return;
    }
    UINavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:viewController];
    if (!viewController.navigationItem.leftBarButtonItem)
    {
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"关闭" style:UIBarButtonItemStylePlain target:viewController action:@selector(dismissModalViewControllerAnimated:)];
    }
    [[self presentingVC] presentViewController:nav animated:YES completion:nil];
}

-(UIViewController *)getVCfromStoryboard:(NSString *)storyBoardName andVCname:(NSString *)VC
{

    UIStoryboard *mystoryboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
    UIViewController *myvc = [mystoryboard instantiateViewControllerWithIdentifier:VC];
    return myvc;
}

#pragma mark Login
- (void)loginOutToLoginVC
{
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
